package com.huawei.appgallery.game.impl;

import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.text.TextUtils;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import com.huawei.updatesdk.service.otaupdate.UpdateKey;
import java.io.File;
import java.util.Locale;
import o.cdx;
import o.cle;
import o.clg;
import o.clj;
import o.cln;
import o.dhq;
import o.dnw;
import o.ContentManager;

public class GameResourceDownloadReceiver extends SafeBroadcastReceiver {

    static class a implements Runnable {
        private long ˋ;

        public a(long j) {
            this.ˋ = j;
        }

        public void run() {
            Cursor query = ((DownloadManager) ContentManager.getInstant().getContext().getSystemService("download")).query(new Query().setFilterById(new long[]{this.ˋ}));
            if (query.moveToFirst()) {
                GameResource ˏ = clg.ˏ().ˏ(this.ˋ);
                if (ˏ == null) {
                    cle.ˎ.ˏ("GameResourcePreLoad", "download finished ,but no such gameResource");
                    return;
                }
                File file = new File(ˏ.ʽ());
                if (query.getInt(query.getColumnIndex(UpdateKey.STATUS)) == 8) {
                    cle.ˎ.ˋ("GameResourcePreLoad", "download succeed, package=" + ˏ.ॱ());
                    if (ˎ(ˏ)) {
                        String ˋ = cln.ˋ(ˏ.ॱ(), ˏ.ˊॱ());
                        if (TextUtils.isEmpty(ˋ)) {
                            ˏ.ˏ(3);
                            cln.ˋ(file);
                        } else {
                            if (ˊ(file, ˋ)) {
                                cle.ˎ.ˋ("GameResourcePreLoad", "moveFile succeed");
                                ˏ.ʼ(ˋ);
                            } else {
                                cle.ˎ.ˏ("GameResourcePreLoad", "moveFile failed");
                                cln.ˋ(file);
                            }
                            long currentTimeMillis = System.currentTimeMillis();
                            ˏ.ˋ(currentTimeMillis);
                            ˏ.ˏ(2);
                            clj.ˋ(ˏ.ॱ(), ˏ.ͺ(), ˏ.ˊॱ(), currentTimeMillis);
                        }
                    } else {
                        cle.ˎ.ˏ("GameResourcePreLoad", "checkFileSha256 failed");
                        ˏ.ˏ(4);
                        cln.ˋ(file);
                    }
                } else {
                    cle.ˎ.ˏ("GameResourcePreLoad", "download failed, package=" + ˏ.ॱ());
                    ˏ.ˏ(3);
                }
                clg.ˏ().ˎ(ˏ);
            }
        }

        private boolean ˊ(File file, String str) {
            if (!file.exists() || TextUtils.isEmpty(str)) {
                return false;
            }
            ˋ(str);
            return file.renameTo(new File(str));
        }

        private void ˋ(String str) {
            try {
                File[] listFiles = new File(str).getParentFile().listFiles();
                if (listFiles != null) {
                    for (File file : listFiles) {
                        if (file.isFile()) {
                            cln.ˋ(file);
                        }
                    }
                }
            } catch (Exception e) {
                cle.ˎ.ˏ("GameResourcePreLoad", "deleteOldFile exception");
            }
        }

        private boolean ˎ(GameResource gameResource) {
            String ˎ = dnw.ˎ(gameResource.ʽ(), AaidIdConstant.SIGNATURE_SHA256);
            if (ˎ == null || gameResource.ˊ() == null) {
                cle.ˎ.ˏ("GameResourcePreLoad", "checkFileSha256 failed:null hash");
                return false;
            }
            boolean equals = ˎ.toLowerCase(Locale.US).equals(gameResource.ˊ());
            cle.ˎ.ˋ("GameResourcePreLoad", "checkFileSha256 result:" + equals);
            return equals;
        }
    }

    public void onReceiveMsg(Context context, Intent intent) {
        if (!dhq.ॱ().ˎ()) {
            cle.ˎ.ˋ("GameResourcePreLoad", " not agree protocol forever,finish!");
        } else if ("android.intent.action.DOWNLOAD_COMPLETE".equals(intent.getAction())) {
            cle.ˎ.ˋ("GameResourcePreLoad", "GameResourceDownloadReceiver.onReceiveMsg");
            long longExtra = intent.getLongExtra("extra_download_id", -1);
            if (longExtra > 0) {
                cdx.ˊ(new a(longExtra));
            }
        }
    }
}
